/*
 * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package top.continew.admin.hanfu.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import net.dreamlu.mica.core.result.R;
import org.springframework.web.bind.annotation.*;
import top.continew.admin.hanfu.domain.PackageType;
import top.continew.admin.hanfu.domain.dto.PackageTypeDto;
import top.continew.admin.hanfu.domain.query.PackageTypeQuery;
import top.continew.admin.hanfu.domain.vo.wx.PackageTypeWxVo;
import top.continew.admin.hanfu.service.impl.PackageTypeServiceImpl;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author bbm
 * @since 2025-03-25
 */
@RestController
@RequestMapping("/package_type")
@RequiredArgsConstructor
public class PackageTypeController {

    private final PackageTypeServiceImpl packageTypeService;

    /**
     * 根据条件查询套餐列表
     * 
     * @param query Type:PackageTypeQuery 查询类
     */
    @PostMapping("/condition")
    public R getPackageTypeServiceListByCondition(@RequestBody PackageTypeQuery query) {

        Page<PackageType> PackageTypes = new Page<>(query.getPageNum(), query.getPageSize());

        if (query.getPackageTypeName() == null && query.getId() == null && query.getStatus() == null) {

            packageTypeService.page(PackageTypes);

        } else {
            packageTypeService.lambdaQuery()
                .eq(query.getId() != null, PackageType::getId, query.getId())
                .like(query.getPackageTypeName() != null, PackageType::getPackageTypeName, query.getPackageTypeName())
                .eq(query.getStatus() != null, PackageType::getStatus, query.getStatus())
                .page(PackageTypes);
        }

        PackageTypes.getRecords().forEach(i -> {
            i.setUpdateTime(DateTime.of(Long.parseLong(i.getUpdateTime())).toString("yyyy-MM-dd HH:mm:ss"));
        });

        return R.success(PackageTypes);
    }

    /**
     * 更加套餐 id获取套餐类型
     */

    @GetMapping("/{id}")
    public R getPackageTypeServiceById(@PathVariable Long id) {
        PackageType packageType = packageTypeService.getById(id);
        return R.success(BeanUtil.copyProperties(packageType, PackageTypeDto.class));
    }

    /**
     * 新增套餐类型列表
     */
    @PostMapping("")
    public R addPackageTypeService(@RequestBody PackageTypeDto req) {

        PackageType packageType = BeanUtil.copyProperties(req, PackageType.class);
        packageType.setUpdateTime(String.valueOf(System.currentTimeMillis()));
        packageType.setId(null);
        packageTypeService.save(packageType);
        return R.success();
    }

    /**
     * 修改接口
     */
    @PutMapping("")
    public R updatePackageTypeService(@RequestBody PackageTypeDto req) {

        PackageType packageType = BeanUtil.copyProperties(req, PackageType.class);
        packageType.setUpdateTime(String.valueOf(System.currentTimeMillis()));
        packageTypeService.updateById(packageType);
        return R.success();
    }

    /**
     * 删除接口
     */
    @DeleteMapping("")
    public R deletePackageTypeService(@RequestBody ArrayList<Integer> ids) {
        packageTypeService.removeByIds(ids);
        return R.success();
    }

    @SaIgnore
    @GetMapping("/wx")
    public R<List<PackageTypeWxVo>> getPackageTypeServiceList() {
        List<PackageTypeWxVo> list = BeanUtil.copyToList(packageTypeService.list(), PackageTypeWxVo.class);
        return R.success(list);
    }

}
